<?php

namespace App\Http\Controllers\admin;

use App\Http\Controllers\Controller;
use App\Models\LaborTeam;
use App\Services\LaborTeamService;
use Illuminate\Http\Request;

class LaborTeamController extends Controller
{
    protected $LaborTeamService;
    public function __construct(LaborTeamService $LaborTeamService)
    {
        $this->LaborTeamService = $LaborTeamService;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $title = $request->title;
        $pageSize = $request->pageSize ?: 10;
        $page = $request->page ?: 1;
        $page = $page * $pageSize - $pageSize;

        $sql = $this->LaborTeamService->get($title);
        $sql->where('company_id',$request->company_id);
        $total = $sql->count();
        $data = $sql->skip($page)->take($pageSize)->orderBy('created_at','desc')->get();
        return response()->json(['code' => 200, 'message' => '操作成功','page'=>$page,'pageSize'=>$pageSize,'total'=>$total,'data'=>$data]);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function all(Request $request)
    {
        $title = $request->title;
        $sql = $this->LaborTeamService->get($title);
        $sql->select('id','name','director','contact_mobile','type','number');
        $sql->where('company_id',$request->company_id);
        $data = $sql->orderBy('created_at','desc')->get();
        return response()->json(['code' => 200, 'message' => '操作成功', 'data'=>$data]);
    }

    public function show(Request $request)
    {
        $data = LaborTeam::find($request->id);
        if($data->birthday){
            $data->birthday = strtotime($data->birthday) * 1000;
        }
        $resources = $data->resources;
        $filesUrl = [];
        foreach ($resources as $k => $v) {
            $arr = [];
            $arr['id'] = $v->id;
            $arr['name'] = $v->resource->name;
            $arr['url'] = $v->resource->url;
            $filesUrl[] = $arr;
        }
        $data->filesUrl = $filesUrl;
        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data]);
    }


    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $POSTONLY = LaborTeam::POSTONLY;
        $input = $request->only($POSTONLY);
        $data = $this->LaborTeamService->insert($input);
        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        $POSTONLY = LaborTeam::POSTONLY;
        $input = $request->only($POSTONLY);
        $input['id'] = $request->id;
        $data = $this->LaborTeamService->update($input);
        return response()->json(['code' => 200, 'message' => '操作成功','data'=>$data]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function delete($id)
    {
        if(!$id){
            return response()->json(['code' => 404, 'message' => '请选择要操作的记录']);
        }
        $data = LaborTeam::find($id);
        $data->delete();
        return response()->json(['code' => 200, 'message' => '操作成功']);
    }



}
